# Maintainer: Yusuke Sasaki <y_sasaki@nuem.nagoya-u.ac.jp>

_realname=gtest
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.8.0
pkgrel=1
pkgdesc="Google Test (mingw-w64)"
arch=('any')
url="https://github.com/google/googletest"
license=('custom:BSD3')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
source=("https://github.com/google/googletest/archive/release-${pkgver}.tar.gz"
        0000-Fix-compilation-of-googletest-with-MinGW-using-Win32-threads.patch
        0001-Fix-Mingw-w64-build.patch)
sha256sums=("58a6f4277ca2bc8565222b3bbd58a177609e9c488e8a72649359ba51450db7d8"
            "a17aca16f399127bf53d453f1d35e8d5e5d42d502fde0d7d100fd1e746998d74"
            "cdfc8e63afe1510a771d2d924cb46e7f69195a60afbeb93f361247bc34cfd7e1")

prepare() {
  cd "${srcdir}/googletest-release-${pkgver}"
  plain "Apply MinGW fixes merged after 1.8.0 release"
  patch -p1 -i "${srcdir}"/0000-Fix-compilation-of-googletest-with-MinGW-using-Win32-threads.patch
  patch -p1 -i "${srcdir}"/0001-Fix-Mingw-w64-build.patch
}

build() {
  cd googletest-release-$pkgver

  rm -rf build
  rm -rf fused-gtest-src
  rm -rf fused-gmock-src
  mkdir build
  cd build

  cmake -G "Unix Makefiles" -DBUILD_SHARED_LIBS=ON -DCMAKE_SKIP_RPATH=ON ..
  make

  ../googletest/scripts/fuse_gtest_files.py ../googletest ../fused-gtest-src
  ../googlemock/scripts/fuse_gmock_files.py ../googlemock ../fused-gmock-src
}

package() {
  cd googletest-release-$pkgver

  mkdir -pm 0755 "$pkgdir"/${MINGW_PREFIX}/{bin,lib,include/gtest/internal/custom,include/gmock/internal/custom,share/licenses/${_realname},src/gtest/src,src/gmock/src,src/gtest/cmake}
  install -m 0644 build/googlemock/gtest/libgtest{,_main}.dll "$pkgdir"/${MINGW_PREFIX}/bin
  install -m 0644 build/googlemock/gtest/libgtest{,_main}.dll.a "$pkgdir"/${MINGW_PREFIX}/lib
  install -m 0644 build/googlemock/libgmock{,_main}.dll "$pkgdir"/${MINGW_PREFIX}/bin
  install -m 0644 build/googlemock/libgmock{,_main}.dll.a "$pkgdir"/${MINGW_PREFIX}/lib
  install -m 0644 googletest/include/gtest/*.h "$pkgdir"/${MINGW_PREFIX}/include/gtest
  install -m 0644 googletest/include/gtest/internal/*.h "$pkgdir"/${MINGW_PREFIX}/include/gtest/internal/
  install -m 0644 googletest/include/gtest/internal/custom/*.h "$pkgdir"/${MINGW_PREFIX}/include/gtest/internal/custom/
  install -m 0644 googlemock/include/gmock/*.h "$pkgdir"/${MINGW_PREFIX}/include/gmock
  install -m 0644 googlemock/include/gmock/internal/*.h "$pkgdir"/${MINGW_PREFIX}/include/gmock/internal/
  install -m 0644 googlemock/include/gmock/internal/custom/*.h "$pkgdir"/${MINGW_PREFIX}/include/gmock/internal/custom/
  install -m 0644 googletest/LICENSE "$pkgdir"/${MINGW_PREFIX}/share/licenses/${_realname}/
  install -m 0644 fused-gtest-src/gtest/* "$pkgdir"/${MINGW_PREFIX}/src/gtest/src/
  install -m 0644 fused-gmock-src/gmock/* "$pkgdir"/${MINGW_PREFIX}/src/gmock/src/
  install -m 0644 fused-gmock-src/*.cc "$pkgdir"/${MINGW_PREFIX}/src/gmock/src/
  install -m 0644 CMakeLists.txt "$pkgdir"/${MINGW_PREFIX}/src/gtest/
  install -m 0644 googletest/cmake/* "$pkgdir"/${MINGW_PREFIX}/src/gtest/cmake/
}

